home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 29 / CDT29.iso / e-Mail / WorldClient Pro 2.2.3 / wcsetup.exe / WEBHELP.ZIP / hhapplet / CanvasButton.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-10-21  |  2.9 KB  |  157 lines

  1. package hhapplet;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Dimension;
  7. import java.awt.Event;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.Rectangle;
  11. import java.awt.image.ImageObserver;
  12. import java.util.Vector;
  13.  
  14. public class CanvasButton extends Canvas {
  15.    protected Rectangle inside;
  16.    protected Image img;
  17.    protected Vector listeners;
  18.    protected boolean button_push_state;
  19.  
  20.    public CanvasButton(Image var1) {
  21.       this();
  22.       this.img = var1;
  23.    }
  24.  
  25.    public CanvasButton() {
  26.       this.button_push_state = false;
  27.       this.img = null;
  28.       this.listeners = new Vector();
  29.       this.button_push_state = false;
  30.       ((Component)this).setBackground(new Color(192, 192, 192));
  31.    }
  32.  
  33.    public void paint(Graphics var1) {
  34.       var1.setColor(((Component)this).getBackground());
  35.       var1.fillRect(0, 0, ((Component)this).bounds().width, ((Component)this).bounds().height);
  36.       if (this.img != null) {
  37.          var1.drawImage(this.img, 2, 2, (ImageObserver)null);
  38.       }
  39.  
  40.       if (this.button_push_state) {
  41.          this.paintBorderIn(var1);
  42.       } else {
  43.          this.paintBorderOut(var1);
  44.       }
  45.    }
  46.  
  47.    public void paintBorderOut(Graphics var1) {
  48.       Rectangle var2 = ((Component)this).bounds();
  49.       Color var3 = ((Component)this).getBackground().brighter();
  50.       Color var4 = var3.darker();
  51.       Color var5 = var4.darker();
  52.       Color var6 = var5.darker();
  53.       var1.setColor(var3);
  54.       var1.drawLine(0, 0, var2.width - 1, 0);
  55.       var1.drawLine(0, 0, 0, var2.height - 1);
  56.       var1.setColor(var4);
  57.       var1.drawLine(1, 1, var2.width - 2, 1);
  58.       var1.drawLine(1, 1, 1, var2.height - 2);
  59.       var1.setColor(var6);
  60.       var1.drawLine(var2.width - 2, 1, var2.width - 2, var2.height - 2);
  61.       var1.drawLine(1, var2.height - 2, var2.width - 2, var2.height - 2);
  62.       var1.setColor(var5);
  63.       var1.drawLine(var2.width - 1, 1, var2.width - 1, var2.height - 1);
  64.       var1.drawLine(0, var2.height - 1, var2.width - 1, var2.height - 1);
  65.    }
  66.  
  67.    public void update(Graphics var1) {
  68.       this.paint(var1);
  69.    }
  70.  
  71.    public void paintBorderIn(Graphics var1) {
  72.       Rectangle var2 = ((Component)this).bounds();
  73.       Color var3 = ((Component)this).getBackground().brighter();
  74.       Color var4 = var3.darker();
  75.       Color var5 = var4.darker();
  76.       Color var6 = var5.darker();
  77.       var1.setColor(var6);
  78.       var1.drawLine(0, 0, var2.width - 1, 0);
  79.       var1.drawLine(0, 0, 0, var2.height - 1);
  80.       var1.setColor(var5);
  81.       var1.drawLine(1, 1, var2.width - 2, 1);
  82.       var1.drawLine(1, 1, 1, var2.height - 2);
  83.       var1.setColor(var4);
  84.       var1.drawLine(var2.width - 2, 1, var2.width - 2, var2.height - 2);
  85.       var1.drawLine(1, var2.height - 2, var2.width - 2, var2.height - 2);
  86.       var1.setColor(var3);
  87.       var1.drawLine(var2.width - 1, 1, var2.width - 1, var2.height - 1);
  88.       var1.drawLine(0, var2.height - 1, var2.width - 1, var2.height - 1);
  89.    }
  90.  
  91.    public void removeButtonPushEventListener(ButtonPushEventListener var1) {
  92.       this.listeners.removeElement(var1);
  93.    }
  94.  
  95.    public Dimension preferredSize() {
  96.       return this.img != null ? new Dimension(this.img.getWidth(this) + 4, this.img.getHeight(this) + 4) : new Dimension(20, 20);
  97.    }
  98.  
  99.    public void addButtonPushEventListener(ButtonPushEventListener var1) {
  100.       this.listeners.addElement(var1);
  101.    }
  102.  
  103.    public boolean handleEvent(Event var1) {
  104.       if (var1.id == 501) {
  105.          this.button_push_state = true;
  106.          ((Component)this).repaint();
  107.          return true;
  108.       } else if (var1.id == 502 && this.button_push_state) {
  109.          this.button_push_state = false;
  110.          ((Component)this).repaint();
  111.          Rectangle var3 = ((Component)this).bounds();
  112.          var3.x = 0;
  113.          var3.y = 0;
  114.          if (var3.inside(var1.x, var1.y)) {
  115.             this.doAction(var1);
  116.          }
  117.  
  118.          return true;
  119.       } else if (var1.id == 506) {
  120.          Rectangle var2 = ((Component)this).bounds();
  121.          var2.x = 0;
  122.          var2.y = 0;
  123.          if (!var2.inside(var1.x, var1.y)) {
  124.             if (this.button_push_state) {
  125.                this.button_push_state = false;
  126.                ((Component)this).repaint();
  127.             }
  128.          } else if (!this.button_push_state) {
  129.             this.button_push_state = true;
  130.             ((Component)this).repaint();
  131.          }
  132.  
  133.          return true;
  134.       } else {
  135.          return false;
  136.       }
  137.    }
  138.  
  139.    protected void doAction(Event var1) {
  140.       ButtonPushEvent var3 = new ButtonPushEvent(this, var1.x, var1.y);
  141.       synchronized(this){}
  142.  
  143.       Vector var2;
  144.       try {
  145.          var2 = (Vector)this.listeners.clone();
  146.       } catch (Throwable var9) {
  147.          throw var9;
  148.       }
  149.  
  150.       for(int var4 = 0; var4 < var2.size(); ++var4) {
  151.          ButtonPushEventListener var5 = (ButtonPushEventListener)this.listeners.elementAt(var4);
  152.          var5.notifyButtonPushEvent(var3);
  153.       }
  154.  
  155.    }
  156. }
  157.